package com.linewell.controller;

import com.linewell.entity.Article;
import com.linewell.entity.UserInfo;
import com.linewell.service.IArticleService;
import com.linewell.service.ITagService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Title: ssm2
 * @description: 文章管理控制器
 * @author: hyuhuang@linewell.com
 * @since:2018/2/10
 */
@Controller
public class ArticleController {

    @Autowired
    IArticleService articleService;
    @Autowired
    ITagService tagService;

    /**    
     * @descripition  插入文章数据
     * @author hyuhuang@linewell.com  
     * @date 2018/2/27 10:32
     * @param userId, content, simcontent, title, articleDate, articlePicture
     * @return com.linewell.utils.Msg  
     */
    @RequestMapping("/addArticle")
    @ResponseBody
    public Msg addArticle(Long userId,String content, String simcontent, String title, String articleDate,String articlePicture){
        Long articleId = System.currentTimeMillis();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        Article article = new Article();
        article.setArticleId(articleId);
        article.setUserId(userId);
        article.setUserInfo(userInfo);
        article.setTitle(title);
        article.setArticleDate(articleDate);
        article.setArticlePicture(articlePicture);
        article.setContent(content);
        article.setSimContent(simcontent);
        return articleService.addArticle(article);
    }
    /**    
     * @descripition  更新文章的赞数量
     * @author hyuhuang@linewell.com  
     * @date 2018/2/27 10:33
     * @param articleId, model
     * @return java.lang.String  
     */
    @RequestMapping(value="/article/{articleId}",method= RequestMethod.GET)
    public String updateArticleZan(@PathVariable("articleId") Long articleId,Model model){
        Article article = articleService.selectArticleWithArticleId(articleId);
        model.addAttribute("article",article);
        return "show";
    }
    /**
     * @descripition  对文章进行排序，且每次取5个数据
     * @author hyuhuang@linewell.com
     * @date 2018/3/15 21:59
     * @param request
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/searchBySort",method = RequestMethod.GET)
    @ResponseBody
    public Msg searchBySort(HttpServletRequest request){
        String sort = request.getParameter("sort");
        Integer page = Integer.valueOf(request.getParameter("page"));
        return articleService.selectBySort(page,sort);
    }
    /**
     * @descripition  随机查询文章
     * @author hyuhuang@linewell.com
     * @date 2018/3/16 20:32
     * @param
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/selectAllArticle",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectAllArticle(){
        List<Article> articleList = articleService.selectAllArticle();
        return Msg.success().add("articleList",articleList);
    }
    /**
     * @descripition  根据关键字搜索文章
     * @author hyuhuang@linewell.com
     * @date 2018/3/27 19:06
     * @param keyword, page
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/searchArticle",method = RequestMethod.GET)
    @ResponseBody
    public Msg searchArticle(@RequestParam("searchType") String searchType,@RequestParam("keyword") String keyword,@RequestParam("page") String page){
        return articleService.searchArticle(searchType,keyword,page);
    }

    /**
     * @descripition  获取最佳文章
     * @author hyuhuang@linewell.com
     * @date 2018/4/3 14:59
     * @param page
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/getBestRank",method = RequestMethod.GET)
    @ResponseBody
    public Msg getBestRank(@RequestParam("page") int page){
        return articleService.getBestRank(page);
    }

    /**
     * @descripition  更新文章
     * @author hyuhuang@linewell.com
     * @date 2018/4/30
     * @param article
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/updateArticle",method = RequestMethod.GET)
    @ResponseBody
    public Msg updateArticle(Article article){
        return articleService.updateArticle1(article);
    }

    /**
     * @descripition 推荐文章
     * @author hyuhuang@linewell.com
     * @date 2018/5/5
     * @param userId
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("recommendArticleByUserId")
    @ResponseBody
    public Msg recommendArticleByUserId(@RequestParam("userId") Long userId){
        return articleService.recommendArticle(userId);
    }
}
